package com.wffz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wffz.comm.utils.DateUtils;
import com.wffz.mapper.IncomeLogMapper;
import com.wffz.po.IncomeLog;
import com.wffz.po.WithdrawRecord;
import com.wffz.service.IncomeLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * @author sunlei
 * @className IncomeLogServiceImpl
 * @Description
 * @date 2023-04-16 6:46
 **/
@Service
@Slf4j
public class IncomeLogServiceImpl implements IncomeLogService {

    @Resource
    private IncomeLogMapper mapper;

    @Override
    public boolean saveInComeLog(IncomeLog incomeLog) {
        return mapper.insert(incomeLog) > 0;
    }

    @Override
    public PageInfo<IncomeLog> queryIncomeLog(Integer pageNum, Integer pageSize, String loginName) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<IncomeLog> wrapper = new QueryWrapper<>();
        wrapper.eq("login_name", loginName);
        wrapper.gt("amount", 0);
        wrapper.orderByDesc("create_time");
        List<IncomeLog> incomeLogs = mapper.selectList(wrapper);
        PageInfo<IncomeLog> pageInfo = new PageInfo<>(incomeLogs);
        return pageInfo;
    }

    @Override
    public float queryAmountByTypes(String[] type, String loginName,boolean isNowDay) {
        QueryWrapper<IncomeLog> wrapper = new QueryWrapper<>();
        wrapper.select("sum(amount) as amount ");
        wrapper.eq("login_name", loginName);
        wrapper.in("income_type", type);
        if (isNowDay) {
            Date start = DateUtils.strToDateLong(DateUtils.dateToStr(new Date(), Locale.CHINA) + " 00:00:00");
            Date end = DateUtils.strToDateLong(DateUtils.dateToStr(new Date(), Locale.CHINA) + " 23:59:59");
            wrapper.between("create_time", start, end);
        }
        IncomeLog in = mapper.selectOne(wrapper);
        if (in != null) {
            return in.getAmount();
        } else {
            return 0;
        }
    }

    @Override
    public float queryAmount(String type, String loginName, boolean isNowDay) {
        QueryWrapper<IncomeLog> wrapper = new QueryWrapper<>();
        wrapper.select("sum(amount) as amount ");
        wrapper.eq("login_name", loginName);
        wrapper.eq("income_type", type);
        if (isNowDay) {
            Date start = DateUtils.strToDateLong(DateUtils.dateToStr(new Date(), Locale.CHINA) + " 00:00:00");
            Date end = DateUtils.strToDateLong(DateUtils.dateToStr(new Date(), Locale.CHINA) + " 23:59:59");
            wrapper.between("create_time", start, end);
        }
        IncomeLog in = mapper.selectOne(wrapper);
        if (in != null) {
            return in.getAmount();
        } else {
            return 0;
        }

    }
}
